/**
 * 
 */
package com.app.mvc.action.rules;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.app.service.IRoleService;
import com.app.service.IUserService;
import com.app.service.vo.UserVo;
import com.app.util.ReuqestToVo;

/**
 * @author yuxk
 *
 */
@Controller
@RequestMapping(value="/admin")
public class AdminAction {

	/**
	 * 
	 */
	public AdminAction() {
		logger.info(this.getClass() + " loder..");
	}
	private static Logger logger = Logger.getLogger(AdminAction.class);
	
	@Autowired
	private IUserService userservice;
	
	@Autowired
	private IRoleService ruleservice;
	
	/**
	 * 展现用户管理页面
	 * @return
	 */
	@RequestMapping(value="/manageUser.form")
	public String manageUser(HttpServletRequest request)
	{
		logger.info("获取所有的用户信息放入到request对象中");
		List<UserVo> uservolist =this.userservice.getAllUser();
		request.setAttribute("uservolist", uservolist);
		return "/logins/admin/login-user-add";
	}
	
	/**
	 * 通过用户id获取用户详细信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/showuserbyid.form")
	public String showUser(@RequestParam("userid") String userid,HttpServletRequest request)
	{
		logger.info("显示用户信息 usrid："+userid);
		request.setAttribute("uservo", this.userservice.loaderUserById(userid));
		return "logins/admin/show-user";
	}
	
	/**
	 * 删除指定用户
	 * @param userid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.form")
	public String deleteUser(@RequestParam("userid") String userid,HttpServletRequest request)
	{
		logger.info("删除用户 userid："+userid);
		UserVo vo = new UserVo();
		vo.setId(userid);
		this.userservice.deleteUserVo(vo);
		return "forward:/admin/manageUser.form";
	}
	
	/**
	 * 更新用户基本信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edituser.form")
	public String editUser(HttpServletRequest request)
	{
		logger.info("更新用户信息");
		UserVo vo = new UserVo();
		ReuqestToVo.getVoFromRequest(request, vo);
		this.userservice.updateUserVo(vo);
		return "forward:/admin/showuserbyid.form?userid="+vo.getId();
	}

	@RequestMapping(value="/adduser.form")
	public String addUser(HttpServletRequest request)
	{
		logger.info("增加新用户");
		UserVo vo = new UserVo();
		ReuqestToVo.getVoFromRequest(request, vo);
		this.userservice.saveUserVo(vo);
		return this.manageUser(request);
	}
	
	/**
	 * 展现用户绑定角色页面
	 * @return
	 */
	@RequestMapping(value="/user-rule.form")
	public String userRule(HttpServletRequest request)
	{
		logger.info("用户绑定角色页面");
		request.setAttribute("user-list", this.userservice.getAllUser());
		request.setAttribute("rule-list", this.ruleservice.getAllRole());
		return "/logins/admin/userrule/userbingrule";
	}
	
	/**
	 * 用户绑定角色
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/rulebing.form")
	public String userBingRule(HttpServletRequest request)
	{
		String roleid = request.getParameter("ruleid");
		String[] userids = request.getParameterValues("userid");
		StringBuilder sb = new StringBuilder();
		for(String s:userids)
		{
			if(!"0".equals(s))
			{
				sb.append(s).append(";");
			}
		}
		List<UserVo> uservolist = this.userservice.getAllUser();
		for(UserVo	vo:uservolist)
		{
			if(sb.toString().indexOf(vo.getId())!=-1)
			{
				vo.setRoleId(roleid);
				this.userservice.updateUserVo(vo);
			}
		}
		return this.userRule(request);
	}
	public IUserService getUserservice() {
		return userservice;
	}

	public void setUserservice(IUserService userservice) {
		this.userservice = userservice;
	}

	public IRoleService getRuleservice() {
		return ruleservice;
	}

	public void setRuleservice(IRoleService ruleservice) {
		this.ruleservice = ruleservice;
	}

	
	
}
